class Dizhen::SchoolsController < Dizhen::BaseController
  before_filter :login_required, :except => [:index, :area, :comments, :photos]
  before_filter :unread_messages_amount
  
  def index
    @schools = School.find(:all, :conditions => "info_source='dizhen.1kg.org' and is_hidden!=1", :select => "id, area_id")
    @photos = Photo.find(:all, :conditions => "photos.school_id<>0 and schools.info_source='dizhen.1kg.org' and schools.is_hidden!=1", 
                               :include => 'school',
                               :limit => 10,
                               :order => "photos.created_at desc")
    @comments = SchoolComment.find(:all, :conditions => "schools.info_source='dizhen.1kg.org' and schools.is_hidden!=1",
                                         :include => 'school',
                                         :limit => 10,
                                         :order => "school_comments.created_at desc")
  end
  
  def new_camp
    @school = School.new(:edu_type => 3, :info_source => 'dizhen.1kg.org') # 临时安置点
  end
  
  def area
    #@schools = School.find(:all, :conditions => ["area_id=? and info_source='dizhen.1kg.org'", params[:id]],
    #                             :include => [:photos, :comments])
    @schools = School.find_by_sql("select schools.id, schools.title, schools.updated_at, count(photos.id) as photo_count, " +
                                          "count(school_comments.id) as comment_count " + 
                                  "from schools " + 
                                  "left join photos on schools.id=photos.school_id " + 
                                  "left join school_comments on schools.id=school_comments.school_id " + 
                                  "where area_id=#{params[:id]} and info_source='dizhen.1kg.org' and is_hidden!=1 " + 
                                  "group by schools.id;")
  end
  
  def photos
    @photos = Photo.paginate(:page => params[:page] || 1,
                             :per_page => 20,
                             :conditions => "photos.school_id<>0 and schools.info_source='dizhen.1kg.org'", 
                             :include => 'school',
                             :order => "photos.created_at desc")
  end
  
  def comments
    @comments = SchoolComment.paginate(:page => params[:page] || 1,
                                       :per_page => 20,
                                       :conditions => "schools.info_source='dizhen.1kg.org'",
                                       :include => 'school',
                                       :order => "school_comments.created_at desc")
  end
  
  private

end